package com.example.goodpet.activity;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Activity;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.example.goodpet.R;
import com.example.goodpet.activity.Fragment.MainChannelFragment;
import com.example.goodpet.base.BaseFragment;
import com.example.goodpet.bean.MainChannel;
import com.example.goodpet.home.fragment.HomeFragment;
import com.example.goodpet.home.homeRecommend.adapter.HomeRecommendFragmentAdapter;
import com.example.goodpet.service.HomeService;
import com.example.goodpet.utils.ActivityManager;
import com.example.goodpet.utils.Constants;
import com.example.goodpet.utils.UIUtils;
import com.zackratos.ultimatebarx.ultimatebarx.java.UltimateBarX;

import java.util.ArrayList;

import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainChannelActivity extends FragmentActivity {
    private String TAG = this.getClass().getSimpleName();

    private int mainChannelId;

    private OkHttpClient okHttpClient;
    private Retrofit retrofit;
    private HomeService homeService;
    private RecyclerView rvSpecificPets;
    private ImageButton back;
    private TextView title;
    private BaseFragment fragment;
    private MainChannel.ResultDTO resultDTO;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_channel);
        UltimateBarX.statusBarOnly(this)
                .fitWindow(true)
                .colorRes(R.color.ic_back_background)
                .light(true)
                .apply();
        ActivityManager.getInstance().addActivity(this);

        initView();
        mainChannelId = getIntent().getIntExtra("mainChannelId",0);
        initData();
        initListener();
    }

    private void initListener() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    private void initData() {

        retrofit = new Retrofit.Builder()
                .baseUrl(Constants.JSON_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        homeService = retrofit.create(HomeService.class);
        initResultDTO();
    }

    private void initResultDTO() {
//        Log.e(TAG, "initResultDTO: "+mainChannelId );
        if(mainChannelId==0){
            homeService.getChannelCatJson().enqueue(new Callback<MainChannel>() {
                @Override
                public void onResponse(Call<MainChannel> call, Response<MainChannel> response) {
                    MainChannel body = response.body();

                    if(body.getCode()==200){
                        resultDTO=body.getResult();
                        initFragment();
                    }else{
                        UIUtils.toast("获取页面失败",false);//json文件获取失败
                    }
                }

                @Override
                public void onFailure(Call<MainChannel> call, Throwable t) {
                    UIUtils.toast("与服务器通信失败！",false);
                }
            });
        }else if(mainChannelId==1){
            homeService.getChannelDogJson().enqueue(new Callback<MainChannel>() {
                @Override
                public void onResponse(Call<MainChannel> call, Response<MainChannel> response) {
                    MainChannel body = response.body();
                    if(body.getCode()==200){
                        resultDTO=body.getResult();
                        initFragment();
                    }else{
                        UIUtils.toast("获取页面失败",false);//json文件获取失败
                    }
                }

                @Override
                public void onFailure(Call<MainChannel> call, Throwable t) {
                    UIUtils.toast("与服务器通信失败！",false);
                }
            });
        }else if(mainChannelId==2){
            homeService.getChannelPetJson().enqueue(new Callback<MainChannel>() {
                @Override
                public void onResponse(Call<MainChannel> call, Response<MainChannel> response) {
                    MainChannel body = response.body();
                    if(body.getCode()==200){
                        resultDTO=body.getResult();
                        initFragment();
                    }else{
                        UIUtils.toast("获取页面失败",false);//json文件获取失败
                    }
                }

                @Override
                public void onFailure(Call<MainChannel> call, Throwable t) {
                    UIUtils.toast("与服务器通信失败！",false);
                }
            });
        }
    }

//

    private void initView() {
//        rvSpecificPets=findViewById(R.id.rv_specific_pets);
        back=findViewById(R.id.titlebar_common_back);
        title=findViewById(R.id.titlebar_common_title);
    }

    private void initFragment() {
        if(resultDTO!=null){
            title.setText(resultDTO.getTitle().getName());
            //初始化Fragment
            fragment = new MainChannelFragment();
            Bundle bundle = new Bundle();
//            bundle.putInt("mainChannelId",mainChannelId);
            bundle.putInt("petId",resultDTO.getTitle().getId());
            bundle.putParcelableArrayList("channelInfo", (ArrayList<? extends Parcelable>) resultDTO.getChannelInfo());
            bundle.putParcelableArrayList("bannerInfo", (ArrayList<? extends Parcelable>) resultDTO.getBannerInfo());
            fragment.setArguments(bundle);
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            if(!fragment.isAdded()){
//                Log.e(TAG, "initAdapter: " );
                transaction.add(R.id.fragment,fragment,MainChannelFragment.class.getName());
                transaction.commit();
            }
        }else{
            Log.e(TAG, "initAdapter: "+"未获取到MainChannel页面数据" );
        }
    }
}